//
//  DataModel.swift
//  ams
//
//  Created by yangyuan on 2017/6/30.
//  Copyright © 2017年 coterjiesen. All rights reserved.
//
import Alamofire
import SwiftyJSON

struct deviceInfo{
    var statusTime: String
    var opTime :String
    var status :Int
    var SN:     String
}
var deviceStatus = [8:"预领",9:"预占",7:"可用",11:"返修",10:"在用",12:"报废",13:"待回收",14:"回收",15:"待出库",16:"在途",17:"待返修",20:"已删除" ,21:"修改"]

class DataModel {
    var deviceList: Array<deviceInfo>?
    var mangerList: Array<String>?
    var identify : String?
    var numAvailDevice: Int = 0
    
    init() {
        self.deviceList = []
        self.mangerList = []
    }
    
    //h获取班主信息
    class func getClassMonInfo(
        username aUsername: String ,
        completionHandler: @escaping (CuValueResponse<DataModel>)->Void
        )->Void{
        let data :DataModel = DataModel();

        Alamofire.request(AMSURL + "queryController/queryStores?username=" + aUsername, method: .post,  headers: MOBILE_CLIENT_HEADERS).responseJSON { response in
            switch response.result {
            case.success(let json):
                let mjson = JSON(json)
                if mjson.isEmpty{
                    return
                }
                var deviceinfo :deviceInfo = deviceInfo(statusTime:"",opTime: "",status: 0 ,SN: "")
                data.identify = mjson["身份类别"].string
                for (_,subjson):(String,JSON) in mjson["staffInfos"]{
                    data.mangerList?.append(subjson["staffame"].string!)
                }
                for (_,subjson):(String,JSON) in mjson["asset"]{
                    deviceinfo.opTime = subjson["opertime"].string!
                    deviceinfo.SN =  subjson["devsno"].string!
                    deviceinfo.status = (Int(subjson["status"].string!))!
                    if deviceinfo.status == 7{
                        data.numAvailDevice += 1
                    }
                    data.deviceList?.append( deviceinfo)
                }
                let t =  CuValueResponse<DataModel>(value:data, success: response.result.isSuccess)
                completionHandler(t);
                
            case.failure(let error):
                print("\(error)")
            }
        }
    }
    class func getDeviceInfo(
        sn: String ,
        completionHandler: @escaping (CuValueResponse<deviceInfo>)->Void
        )->Void{
        Alamofire.request(AMSURL + "queryController/queryAsset?SN=" + sn, method: .post,  headers: MOBILE_CLIENT_HEADERS).responseJSON { response in
            switch response.result {
            case.success(let json):
                let subjson = JSON(json)
                var deviceinfo :deviceInfo = deviceInfo(statusTime:"",opTime: "",status: 0 ,SN: "")
                if subjson.isEmpty{
                    let t =  CuValueResponse<deviceInfo>(value:deviceinfo, success: response.result.isSuccess)
                    completionHandler(t);
                    return
                }
                deviceinfo.statusTime = subjson["statustime"].string!
                deviceinfo.opTime = subjson["opertime"].string!
                deviceinfo.SN =  subjson["devsno"].string!
                deviceinfo.status = (Int(subjson["status"].string!))!
                let t =  CuValueResponse<deviceInfo>(value:deviceinfo, success: response.result.isSuccess)
                completionHandler(t);
                
            case.failure(let error):
                print("\(error)")
            }
        }
    }

}
